<template>
  <el-dialog
    :title="!dataForm.deptId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="部门名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="deptSort">
        <!-- <el-input v-model="dataForm.deptSort" placeholder="排序"></el-input> -->
        <el-input-number
          v-model.number="dataForm.deptSort"
          :min="0"
          :max="999"
          controls-position="right"
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="enabled">
        <el-radio-group v-model="dataForm.enabled">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        style="margin-bottom: 0;"
        label="上级部门"
        prop="parentName"
      >
        <el-popover
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click"
        >
          <el-tree
            :data="deptList"
            :props="deptListTreeProps"
            node-key="deptId"
            ref="menuListTree"
            @current-change="deptListTreeCurrentChangeHandle"
            :highlight-current="true"
            :expand-on-click-node="false"
          >
          </el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:menuListPopover
          :readonly="true"
          placeholder="点击选择上级菜单"
          class="menu-list__input"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        parentName: "一级部门",
        isTop: "1",
        deptId: 0,
        pid: 0,
        subCount: "",
        name: "",
        deptSort: 999,
        enabled: 1,
        createUserId: "",
        updateUserId: "",
        createTime: "",
        updateTime: ""
      },
      deptListTreeProps: {
        children: "children",
        label: "name"
      },
      deptList: [],
      dataRule: {
        parentName: [
          { required: true, message: "上级部门不能为空", trigger: "change" }
        ],
        subCount: [
          { required: true, message: "子部门数目不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 255,
            message: "最多输入255个字符"
          }
        ],
        deptSort: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        enabled: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        createUserId: [
          { required: true, message: "创建者ID不能为空", trigger: "blur" }
        ],
        updateUserId: [
          { required: true, message: "更新者ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建日期不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    user: {
      get() {
        return this.$store.state.user;
      }
    }
  },
  methods: {
    //部门树选中
    deptListTreeCurrentChangeHandle(data, node) {
      debugger;
      this.dataForm.pid = data.deptId;
      this.dataForm.parentName = data.name;
    },
    init(id) {
      this.dataForm.deptId = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/dept/select"),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.deptList = data.deptList;
        console.log("this.deptList", this.deptList);
        this.visible = true;
        this.$nextTick(() => {
          this.$refs["dataForm"].resetFields();
        });
        if (this.dataForm.deptId) {
          this.$http({
            url: this.$http.adornUrl(`/sys/dept/info/${this.dataForm.deptId}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              debugger;
              this.dataForm.pid = data.sysDept.pid;
              this.dataForm.subCount = data.sysDept.subCount;
              this.dataForm.name = data.sysDept.name;
              this.dataForm.parentName = data.sysDept.name;
              this.dataForm.deptSort = data.sysDept.deptSort;
              this.dataForm.enabled = data.sysDept.enabled;
              this.dataForm.createUserId = data.sysDept.createUserId;
              this.dataForm.updateUserId = data.sysDept.updateUserId;
              this.dataForm.createTime = data.sysDept.createTime;
              this.dataForm.updateTime = data.sysDept.updateTime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.createUserId = this.user.id;
      this.dataForm.updateUserId = this.user.id;
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/sys/dept/${!this.dataForm.deptId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              deptId: this.dataForm.deptId || undefined,
              pid: this.dataForm.pid,
              subCount: this.dataForm.subCount,
              name: this.dataForm.name,
              deptSort: this.dataForm.deptSort,
              enabled: this.dataForm.enabled,
              createUserId: this.dataForm.createUserId,
              updateUserId: this.dataForm.updateUserId,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
